package forms;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;

import model.StudentModel;

import org.apache.struts.action.*;

import db.DbException;

public class ReviewerFormInsert extends ActionForm
{
	private static final long serialVersionUID = 1L;
	
	private String username = "";
	private String password = "";
	private boolean uErr = false;
	
	public String getUsername() {
		return username;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	/**
	 * Reset all properties to their default values.
	 * 
	 * @param mapping
	 *            The mapping used to select this instance
	 * @param request
	 *            The servlet request we are processing
	 */
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		setUsername("");
		setPassword("");
	}

	/**
	 * Validate the input data. If validation fails, then report the errors in
	 * students.jsp, and specifically where <html:errors/> is located.
	 * 
	 * @param mapping
	 *            The mapping used to select this instance
	 * @param request
	 *            The servlet request we are processing
	 */
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request)
	{
		ActionErrors errors = new ActionErrors();

		if( username.equals("") )
		{
			errors.add("username", new ActionMessage("error.revname.required"));
			setUsername("");
		}
		if( password.equals(""))
		{
			errors.add("password", new ActionMessage("error.revpwd.required"));
			setPassword("");
		}
		
		String url = "jdbc:postgresql://localhost:5432/Applications";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;

		try {
			// Registering Postgresql JDBC driver
			Class.forName("org.postgresql.Driver");
			// Open a connection to the database	
			conn = DriverManager.getConnection(url,"postgres","postgres");
			// Create the statement
		    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
		                				ResultSet.CONCUR_UPDATABLE);
		    	    
			if( username.equals("") )
			{
				errors.add("username", new ActionMessage("error.username.required"));
				setUsername("");
			}
			else
			{
				// make sure the username hasn't already been used
				rs = stmt.executeQuery("SELECT username FROM Reviewers");
				while( rs.next() )
				{
					if( username.equals(rs.getString("username")) )
					{
						uErr = true;
					}
				}
			}
			
			if( uErr )
			{
				errors.add("username", new ActionMessage("error.revname.duplicate"));
				uErr = false;
				setUsername("");
			}
		
			// close connection
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) 
		{ 
			System.out.println(e);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return errors;
	}

}
